#region history
//************************************************************************************
// 20080104/WMB/LineContentCollection.cs created for Mitosoft.
// 
// Remarks, Restrictions
// ===================================================================================
//
//************************************************************************************
#endregion

#region using declarations

using System.Collections.Generic;
using System.Linq;

#endregion

namespace Mitosoft.TextFileReader {
  
  /// <summary>
  /// Represents a collection holding line contents.
  /// </summary>
  public class LineContentCollection : List<ILineContent> {

    #region Fields
    #endregion

    #region Constructor(s)
    
    /// <summary>
    /// Creates a new instance of a LineContenCollection object
    /// </summary>
    public LineContentCollection() {
      // Intentionally left blank
    }
    
    #endregion

    #region Properties
    
    /// <summary>
    /// Returns the line number of the last line in the collection. 
    /// We assume that the collection items are ordered by the line number.
    /// So this property should return the biggest line number of the collection.
    /// If no lines exists, null will be returned.
    /// </summary>
    public ulong? LastLineNumber {
      get
      {
        if (!this.Any())
          return null;

        return this[Count-1].LineNumber;
      }
    }
    
    /// <summary>
    /// Returns the line number of the first line in the collection. 
    /// We assume that the collection itmes are ordered by the line number.
    /// So this property should return the smallest line number of the collection.
    /// If no lines exists, null will be returned.
    /// </summary>
    public ulong? FirstLineNumber 
    {
      get
      {
        if (!this.Any())
          return null;

        return this[0].LineNumber;
      }
    }
    
    #endregion

    #region Methods
    
    /// <summary>
    /// Returns the index of the element with the given line number.
    /// </summary>
    /// <param name="lineNumber">Line number to be found</param>
    /// <returns>Found index or null if nothing was found.</returns>
    public uint? Index(ulong? lineNumber) {
      
      if (Count == 0) return null;
      
      for(uint i=0; i < Count; i++)
        if (this[(int)i].LineNumber == lineNumber)
          return i;

      return null;  // Nothing found
    }

    #endregion

  }
}
